<?php

class ArticlesController extends FController {
	public function actionView($id) {
		$this->render('view', array(
			'model' => $this->loadModel($id, 'Article'),
		));
	}
	protected function performAjaxValidation($model, $form = null) {
		if (Yii::app()->getRequest()->getIsAjaxRequest() && (($form === null) || ($_POST['ajax'] == $form))) {
			echo GxActiveForm::validate($model);
			Yii::app()->end();
		}
	}

	public function actionCreate() {
		$model = new Article;
		if(currentUser() == false){
			$this->redirect(array('/users/login'));
		}
		
		$criteria = new CDbCriteria;
		$criteria->compare('user_id', Yii::app()->user->id, true);
		$datas=UserCategory::model()->findAll($criteria);
		if(!count($datas)) {
			Yii::app()->user->setFlash('success', 'You are not belong to any category! You need join categories first (maximun is 3 category)');
			$this->redirect(array('/users/categoryAuthor'));
		}
		$this->performAjaxValidation($model, 'article-form');

		if (!empty($_POST['Article'])) {
			$model->attributes = $_POST['Article'];
			$model->user_id = currentUser()->id;
			$model->country_id = Country::model()->visitorCountryID();
			$model->created = date("Y-m-d G:i:s");
			$model->modified = date("Y-m-d G:i:s");
			// $model->image = null;
			if ($model->save()) {
				if (Yii::app()->getRequest()->getIsAjaxRequest())
					Yii::app()->end();
				else{
					$this->redirect(array('/articles/me'));
				}
			}
		}

		$this->render('create', array( 'model' => $model));
	}

	public function actionUpdate($id) {
		$model = Article::model()->findByPk($id);
		$this->performAjaxValidation($model, 'article-form');

		if (isset($_POST['Article'])) {
			$revision = new RevisionArticle;
			$revision->attributes = $model->attributes;
			$revision->p_id = $model->id;
			$revision->user_id = $model->user_id;
			$revision->alias = $model->alias;
			$revision->title = $model->title;
			$revision->description = $model->description;
			$revision->content = $model->content;
			$revision->priority = $model->priority;
			$revision->country_id = $model->country_id;
			$revision->category_id = $model->category_id;
			$revision->verified_count = $model->verified_count;
			$revision->is_published = $model->is_published;
			$revision->source = $model->source;
			$revision->created = date("Y-m-d G:i:s");
			$revision->modified = date("Y-m-d G:i:s");
			
			if($revision->save()){
			
			}else{
				$errors  = $revision->getErrors();
				list ($field, $_errors) = each ($errors);
				Yii::app()->user->setFlash('error', 'Article updated failer.'.$_errors[0]);
				$this->redirect(array('update', 'id' => $model->id));
			}
		
			$model->setAttributes($_POST['Article']);
			$model->user_id = currentUser()->id;
			if ($model->save()) {
				
				Yii::app()->user->setFlash('success', 'Article updated.');
				$this->redirect(array('update', 'id' => $model->id));
			}
		}
		
		$model = Article::model()->findByPk($id);
		
		
		$criteria=new CDbCriteria;
		$criteria->compare('is_published', 1, true);
		$criteria->compare('p_id', $model->id, true);
		$criteria->order = "created DESC";
	
		$dataProvider=new CActiveDataProvider('RevisionArticle', array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=> 1000,
				'pageVar'=>'page',
			),
		));
		
		
		$this->render('update', array(
				'model' => $model,
				'dataProvider' => $dataProvider,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadModel($id, 'Article')->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('Article');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		$model = new Article('search');
		$model->unsetAttributes();

		if (isset($_GET['Article']))
			$model->setAttributes($_GET['Article']);

		$this->render('admin', array(
			'model' => $model,
		));
	}

}